package ott.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TimeZone;

import oot.activity.ProfileActivity;
import oot.activity.SingleTweetActivity;
import oot.data.Connection;
import oot.data.Tweet;
import oot.tab.activity.R;
import oot.utils.StringUtil;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidquery.AQuery;

public class TweetAdapter extends ArrayAdapter<Tweet> {
	private LayoutInflater mInflater;
	private Context mContext;
	private ArrayList<Tweet> filteredModelItemsArray = new ArrayList<Tweet>();

	// private AQuery mA
	public TweetAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		// TODO Auto-generated constructor stub
	}

	public TweetAdapter(Context context, ArrayList<Tweet> objects) {
		super(context, 0, objects);
		this.filteredModelItemsArray = objects;
		mContext = context;
		mInflater = LayoutInflater.from(context);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		// Log.i("test","adapter view");
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(
					oot.tab.activity.R.layout.tweet_single_row, null);
			holder.avatar = (ImageView) convertView
					.findViewById(oot.tab.activity.R.id.img_avata);
			holder.content = (TextView) convertView
					.findViewById(oot.tab.activity.R.id.tv_content);
			holder.timeSince = (TextView) convertView
					.findViewById(oot.tab.activity.R.id.tv_since);
			holder.typeOfTweet = (ImageView) convertView
					.findViewById(oot.tab.activity.R.id.img_tweet_type_tweet);
			holder.typeOfTweetContent = (ImageView) convertView
					.findViewById(oot.tab.activity.R.id.img_tweet_type);
			holder.userName = (TextView) convertView
					.findViewById(oot.tab.activity.R.id.tv_user_name);
			holder.userScreenName = (TextView) convertView
					.findViewById(oot.tab.activity.R.id.tv_screen_name);
			holder.retweetByUser = (TextView) convertView
					.findViewById(oot.tab.activity.R.id.tv_tweet_row_retweet_user);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final Tweet data = filteredModelItemsArray.get(position);
		if (data.getType().equals(Tweet.TYPE_REPLY))
			holder.typeOfTweetContent
					.setImageResource(R.drawable.conversation_icon);
		else if(data.getImgURL().length()>1){
			holder.typeOfTweetContent
			.setImageResource(R.drawable.image_icon);
		}
		else{
			holder.typeOfTweetContent.setImageResource(R.drawable.no_thing);
		}
		AQuery queryAvatar = new AQuery(convertView);
		if (data.getType().equals(Tweet.TYPE_RETWEET)) {
			queryAvatar
					.id(R.id.img_avata)
					.visible()
					.image(Connection.BASE_URL_IMAGE+data.getRetweet().getUser().getUserAvatarURL(),
							true, true, 50, R.drawable.m05_item_avar);
			holder.avatar.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(mContext, ProfileActivity.class);
					intent.putExtra("user", data.getUser());
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					mContext.startActivity(intent);
				}
			});
			holder.userName.setText("@"
					+ data.getRetweet().getUser().getUserName());
			holder.userScreenName.setText(data.getRetweet().getUser()
					.getUserScreenName());
			holder.content.setText(StringUtil.validString(data.getRetweet()
					.getContent(), 40));
			holder.retweetByUser.setText("Retweeted by @"
					+ data.getUser().getUserName());
		} else {
			queryAvatar
					.id(R.id.img_avata)
					.visible()
					.image(Connection.BASE_URL_IMAGE+data.getUser().getUserAvatarURL(), true, true, 50,
							R.drawable.m05_item_avar);
			holder.avatar.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(mContext, ProfileActivity.class);
					intent.putExtra("user", data.getUser());
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					mContext.startActivity(intent);
				}
			});
			holder.userName.setText("@" + data.getUser().getUserName());
			holder.userScreenName.setText(data.getUser().getUserScreenName());
			holder.content
					.setText(StringUtil.validString(data.getContent(), 40));
			holder.retweetByUser.setText("");
		}

		// only favorite
		holder.timeSince.setText(convertTimeSince(data.getTimeSince(),
				data.getCreatedDate()));

		if (data.getFavorite().equals(Tweet.FAVORITE)
				&& !data.getReTweetedByMe().equals(Tweet.RETWEETED_IS_BY_ME)) {
			holder.typeOfTweet.setImageResource(R.drawable.dogear_fave_icon);

		}
		// only retweet
		else if (!data.getFavorite().equals(Tweet.FAVORITE)
				&& data.getReTweetedByMe().equals(Tweet.RETWEETED_IS_BY_ME)) {
			holder.typeOfTweet.setImageResource(R.drawable.dogear_rt_icon);
		} else if (data.getFavorite().equals(Tweet.FAVORITE)
				&& data.getReTweetedByMe().equals(Tweet.RETWEETED_IS_BY_ME)) {
			holder.typeOfTweet.setImageResource(R.drawable.dogear_both_icon);
		} else {
			holder.typeOfTweet.setImageResource(R.drawable.no_thing);
		}
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent i = new Intent(mContext, SingleTweetActivity.class);
				i.putExtra("TWEET", data);
				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				mContext.startActivity(i);
			}
		});
		convertView.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		return convertView;
	}

	

	private class ViewHolder {
		ImageView typeOfTweet; // retweet|favorite
		ImageView typeOfTweetContent; // conversation| media
		ImageView avatar;
		TextView content;
		TextView timeSince;
		TextView userName;
		TextView userScreenName;
		TextView retweetByUser;
	}

	private String convertTimeSince(String time, String createdTime) {
		String result = "";
		String splitString[] = time.split(":");
		if (splitString[0].equals("00") && splitString[1].equals("00"))
			result = splitString[2] + "s";
		else if (splitString[0].equals("00") && !splitString[1].equals("00"))
			result = splitString[1] + "m";
		else if (Integer.parseInt(splitString[0]) < 24)
			result = splitString[0] + "h";
		else {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

			sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
			try {
				Date tempDate = sdf.parse(createdTime);
				// tempDate.
				SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd");
				result = sdf2.format(tempDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}
}
